using System;
using System.Diagnostics;
using System.Windows.Forms;
using mshtml;
using OFX.Aggregator.WebScraping.Infrastructure;

namespace OFX.Aggregator.WebScraping.Implementation
{
	public class WebBrowserWrapper : IWebBrowser, IDisposable
	{
		private readonly HtmlDocumentWrapper documentWrapper;
		private readonly WebBrowser webBrowser;

		[DebuggerStepThrough]
		public WebBrowserWrapper(WebBrowser webBrowser)
		{
			this.webBrowser = webBrowser;
			this.documentWrapper = new HtmlDocumentWrapper(webBrowser.Document);
		}

		#region Implementation of IWebBrowser

		public IHtmlDocument Document
		{
			[DebuggerStepThrough]
			get { return this.documentWrapper; }
		}

		public Uri Url
		{
			[DebuggerStepThrough]
			get { return this.webBrowser.Url; }
		}

		public void Navigate(string url)
		{
			HtmlDocument document = this.webBrowser.Document;

			if (document == null)
				throw new InvalidOperationException();

			((HTMLDocumentClass) document.DomDocument).parentWindow.navigate(url);
		}

		#endregion

		#region Implementation of IDisposable

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		/// <filterpriority>2</filterpriority>
		public void Dispose()
		{
			this.documentWrapper.Dispose();
		}

		#endregion
	}
}